package com.kingyea.mobilepolice.usermenu.service.impl;

import com.kingyea.mobilepolice.usermenu.bean.UserMenu;
import com.kingyea.mobilepolice.usermenu.bean.UserMenuCondition;
import com.kingyea.mobilepolice.usermenu.bean.UserMenuPo;
import com.kingyea.mobilepolice.usermenu.dao.UserMenuMapper;
import com.kingyea.mobilepolice.usermenu.service.UserMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hx
 * @version v1.0.0
 * @description UserMenu 实现类
 * @date Created in 2018-04-17 15:15:36
 */
@Service
@Transactional
public class UserMenuServiceImpl implements UserMenuService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserMenuServiceImpl.class);

    @Resource
    private UserMenuMapper userMenuMapper;

    /**
     * 通过ID查找对象
     *
     * @return
     */
    @Override
    public UserMenu getById() throws Exception {
        return this.userMenuMapper.getById();
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public List<UserMenu> search(UserMenuCondition condition) throws Exception {
        List<UserMenu> list = userMenuMapper.search(condition);
        return list;
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @Override
    public int deleteById(String userId) throws Exception {
        userMenuMapper.deleteById(userId);
        return 1;
    }

    /**
     * 保存
     *
     * @param userMenu
     * @return
     */
    @Override
    public UserMenu save(UserMenu userMenu) throws Exception {
        try {
            userMenuMapper.save(userMenu);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userMenu;
    }

    /**
     * 更新
     *
     * @param userMenu
     * @return
     */
    @Override
    public UserMenu update(UserMenu userMenu) throws Exception {
        try {
            userMenuMapper.update(userMenu);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userMenu;
    }

    @Override
    public boolean addMenuForUser(UserMenuPo po) {
        Integer i = 0;
        userMenuMapper.deleteById(po.getUserId());
        for (Integer menuId : po.getMenuIds()) {
            UserMenu userMenu = new UserMenu();
            userMenu.setUserId(po.getUserId());
            userMenu.setMenuId(menuId);
            userMenu.setIsAdd(0);
            userMenuMapper.save(userMenu);
            i++;
        }
        return i.equals(po.getMenuIds().size());
    }

}
